package com.truckdispatching.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.TextView;

import com.truckdispatching.R;
import com.truckdispatching.http.controller.LoginHttpController;
import com.truckdispatching.http.util.OnResponseHandler;
import com.truckdispatching.upgrade.UpgradeManager;
import com.truckdispatching.util.SharedPreferencesUtil;

/**
 * 登陆界面
 * 
 * @author Lee
 * @date 2013-4-30
 */
public class LoginActivity extends BaseActivity implements OnClickListener {

	private TextView mAccountTextView;
	private TextView mPasswordTextView;
	private View mBtnLogin;
	private CheckBox mRemenberAccount;
	private TextView mBtnRegister;
	private View mBtnPhone;

	private LoginHttpController mLoginHttpController;
	private OnResponseHandler mOnResponseHandler;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login_activity);
		SharedPreferencesUtil.getInstance().initWithApplication(getApplication());
		UpgradeManager.getInstance().checkVersion(true, this);
		mAccountTextView = (TextView) findViewById(R.id.login_account);
		mPasswordTextView = (TextView) findViewById(R.id.login_password);
		mBtnLogin = findViewById(R.id.btn_login);
		mBtnLogin.setOnClickListener(this);
		mRemenberAccount = (CheckBox) findViewById(R.id.remenber_account);
		mBtnRegister = (TextView) findViewById(R.id.register);
		mBtnRegister.setOnClickListener(this);
		mBtnPhone = findViewById(R.id.phone);
		mBtnPhone.setOnClickListener(this);

		String account = SharedPreferencesUtil.getInstance().getUsername();
		String password = SharedPreferencesUtil.getInstance().getPassword();
		if (!account.equals("") && !password.equals("")) {
			mAccountTextView.setText(account);
			mPasswordTextView.setText(password);
		}
		mRemenberAccount.setChecked(true);

		mOnResponseHandler = new OnResponseHandler() {

			@Override
			public void onResponse(Object response) {
				if (mRemenberAccount.isChecked()) {
					String account = mAccountTextView.getText().toString();
					String password = mPasswordTextView.getText().toString();
					SharedPreferencesUtil.getInstance().setUsername(account);
					SharedPreferencesUtil.getInstance().setPassword(password);
				} else {
					SharedPreferencesUtil.getInstance().setUsername("");
					SharedPreferencesUtil.getInstance().setPassword("");
				}
				dismissLoadingDialog();
				showToast("登陆成功");
				// new GetUserInfoHttpController().sendRequest();
				Intent intent = new Intent(LoginActivity.this, MainActivity.class);
				startActivity(intent);
				finish();
			}

			@Override
			public void onResponseError(int errorCode, String errorMessage) {
				dismissLoadingDialog();
				if (errorMessage != null && !errorMessage.equals("")) {
					showToast("登陆失败：" + errorMessage);
				} else {
					showToast("登陆失败");
				}
			}
		};
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_login:
			String account = mAccountTextView.getText().toString().trim();
			String password = mPasswordTextView.getText().toString().trim();

			if (account.equals("")) {
				showToast("帐号不能为空");
				mAccountTextView.requestFocus();
				return;
			}
			if (password.equals("")) {
				showToast("密码不能为空");
				mPasswordTextView.requestFocus();
				return;
			}
			if (account.length() != 11) {
				showToast("帐号格式错误");
				mAccountTextView.requestFocus();
				return;
			}
			if (!account.startsWith("1")) {
				showToast("帐号格式错误");
				mAccountTextView.requestFocus();
				return;
			}
			// 发送网络请求，判断账号密码是否正确
			if (mLoginHttpController != null) {
				mLoginHttpController.cancelHttpRequest();
				mLoginHttpController = null;
			}
			mLoginHttpController = new LoginHttpController();
			mLoginHttpController.setOnResponseListener(mOnResponseHandler);
			mLoginHttpController.sendRequest(account, password);
			showLoadingDialog();
			break;
		case R.id.register:
			Intent intent = new Intent(this, RegisterAcivity.class);
			startActivity(intent);
			finish();
			break;
		case R.id.phone: {
			Uri callUri = Uri.parse("tel:4000566560");
			Intent i = new Intent(Intent.ACTION_CALL, callUri);
			startActivity(i);
			break;
		}
		default:
			break;
		}

	}

}
